package com.gadgets.test.suite;

import com.alibaba.druid.pool.DruidDataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.sql.DataSource;
import javax.sql.PooledConnection;
import java.sql.SQLException;
import java.util.List;

/**
 * 功能说明：
 *
 * @author Unclexiu
 * @version V202101.00.000
 * @data 2022-09-22
 * 修改记录：  修改日期          修改人员    修改说明
 * 2022-09-22  hspcadmin   创建文件
 */
@Component
@Transactional(transactionManager = "gadgetsTestDbTransactionManage")
public class DataExcutor {

	protected final Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	@Qualifier("gadgetsDbTestSource")
	public DataSource dataSource;

	@Transactional(transactionManager = "gadgetsTestDbTransactionManage", rollbackFor = Throwable.class)
	public int excuteGdagetsInsertSQL(List<String> sql) throws SQLException {
		int retNum = 0;
		for (String s : sql) {
			logger.info("数据准备/清理执行SQL：{}", s);
			try {
				PooledConnection connection = ((DruidDataSource)dataSource).getPooledConnection();
				retNum += connection.getConnection()
						.prepareStatement(s)
						.executeUpdate();
			} catch (SQLException e) {
				logger.error("SQL：{}执行报错", s);
			}
		}
		return retNum;
	}

}
